package com.itboye.ihomebank.adapter;

import android.content.Context;
import android.view.View;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.BeiFenKeyBean;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.ttKey.DbService;
import com.itboye.ihomebank.ttKey.Key;
import com.itboye.ihomebank.ttKey.MyPreference;

import java.util.List;

/**
 * 备份钥匙列表
 */
public class BeiFenKeyAdapter extends MutiplyCommonAdapter<BeiFenKeyBean> implements View.OnClickListener {
	int position;
	List<BeiFenKeyBean> keyArray;

	AdapterOrderFace orderFace;

	public BeiFenKeyAdapter(Context context, List<BeiFenKeyBean> datas, AdapterOrderFace orderFace, int... layoutId) {
		super(context, datas, layoutId);
		keyArray = datas;
		this.orderFace = orderFace;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getItemPosition(int position) {
		this.position = position;
		
	}

	@Override
	public void convert(ViewHolder holder, BeiFenKeyBean t, int type, int position) {

		holder.setText(R.id.my_key_type, t.getKeyId());
		holder.setVisible(R.id.item_my_zhuangtai, false);

		String accessToken = MyPreference.getStr(mContext, MyPreference.ACCESS_TOKEN);
		Key localKey = DbService.getKeyByAccessTokenAndLockmac(accessToken, t.getLockMac());

		if(localKey != null&&!localKey.getLockMac().equals("")) {
			holder.setVisible(R.id.item_my_down, false);
			holder.setText(R.id.my_key_name, t.getLockAlilas()+"    (已下载)");
		}else{
			holder.setVisible(R.id.item_my_down, true);
			holder.setText(R.id.my_key_name, t.getLockAlilas()+"    (未下载)");
		}
		holder.setOnClickListener(R.id.item_my_down, position,this);
		holder.setTag(R.id.item_my_down, position);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.item_my_down:
				orderFace.onDown(v);
				break;
		}
	}

	public interface AdapterOrderFace {
		void onDown(View view);//下载钥匙
	}

}
